package com.chuangke.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chuangke.common.constant.CacheKeyConstants;
import com.chuangke.system.dao.SysConfigDao;
import com.chuangke.system.entity.SysConfig;
import com.chuangke.system.service.SysConfigService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class SysConfigServiceImpl implements SysConfigService {

	@Autowired
	private SysConfigDao sysConfigDao;

	@CacheEvict(cacheNames = CacheKeyConstants.SYS_CONFIG, key = "'" + CacheKeyConstants.SYS_CONFIG + "'")
	@Override
	public void save(SysConfig record) {
		delete(record.getOwner(),record.getParamKey()) ;
		sysConfigDao.saveOrUpdate(record) ;
	}

	@CacheEvict(cacheNames = CacheKeyConstants.SYS_CONFIG, key = "'" + CacheKeyConstants.SYS_CONFIG + "'")
	@Override
	public void delete(String id) {
		sysConfigDao.remove(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getId, id));
	}

	@CacheEvict(cacheNames = CacheKeyConstants.SYS_CONFIG, key = "'" + CacheKeyConstants.SYS_CONFIG + "'")
	@Override
	public void delete(String owner, String key) {
		sysConfigDao.remove(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getOwner, owner).eq(SysConfig::getParamKey, key));
	}

	@CacheEvict(cacheNames = CacheKeyConstants.SYS_CONFIG, key = "'" + CacheKeyConstants.SYS_CONFIG + "'")
	@Override
	public void update(SysConfig record) {
		sysConfigDao.update(record, new UpdateWrapper<SysConfig>().lambda().eq(SysConfig::getId, record.getId()).eq(SysConfig::getOwner, record.getOwner()));
	}

	@Cacheable(cacheNames = CacheKeyConstants.SYS_CONFIG, key = "'" + CacheKeyConstants.SYS_CONFIG + "'")
	@Override
	public Map<String, SysConfig> findAll() {
		List<SysConfig> list = sysConfigDao.list(new QueryWrapper<>());
		return list == null ? new HashMap<>()
				: list.stream().collect(Collectors.toMap(config -> config.getOwner() + "-" + config.getParamKey(), a -> a));
	}

	@Override
	public List<SysConfig> list(String type) {
		Map<String, SysConfig> map = ((SysConfigService) AopContext.currentProxy()).findAll();
		List<SysConfig> list = map.values().stream().collect(Collectors.toList()) ;
		if(!StringUtils.isEmpty(type)){
			list =list.stream().filter(sysConfig -> type.equals(sysConfig.getType())).collect(Collectors.toList()) ;
		}
		return list ;
	}

	@Override
	public SysConfig find(String owner, String key) {
		Map<String, SysConfig> map = ((SysConfigService) AopContext.currentProxy()).findAll();
		return map == null ? null : map.get(owner + "-" + key);
	}

	@Override
	public String find(String owner, String key, String defaultValue) {
		SysConfig config = find(owner,key) ;
		return config == null ? defaultValue : config.getParamValue() ;
	}

}
